package mysql

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

var (
	server *SvMySql
	isInit = false
)

const DB_NAME = ""
const formatSt = "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local"

func GetDB(name string) *gorm.DB {
	if !isInit {
		println("[INFO] mysql name:", name)
		InitMysql()
		isInit = true
	}
	return server.Engine()
}

func initNewServer() {
	server = new(SvMySql)
	server.URL = fmt.Sprintf(formatSt, MysqlConfig.MysqlUser, MysqlConfig.MysqlPwd,
		MysqlConfig.MysqlHost, MysqlConfig.MysqlPort, MysqlConfig.MysqlDBName)
	server.NewEngine()
	// debug
	server.engine.LogMode(true)
}

func InitMysql() {
	initEnv()
	initNewServer()
}
